package com.cysion.study.ui.personal

import android.content.Intent
import android.net.Uri
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import com.cysion.other.color
import com.cysion.study.R
import com.cysion.study.adapter.ActivityDetailItemAdapter
import com.cysion.study.entity.Studentlist
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import kotlinx.android.synthetic.main.activity_activity_detail.*
import kotlinx.android.synthetic.main.activity_activity_detail.ll_phone2
import kotlinx.android.synthetic.main.activity_activity_detail.rv_list
import kotlinx.android.synthetic.main.activity_activity_detail.topbar
import kotlinx.android.synthetic.main.activity_activity_detail.tv_address
import kotlinx.android.synthetic.main.activity_activity_detail.tv_phone1
import kotlinx.android.synthetic.main.activity_activity_detail.tv_phone2
import kotlinx.android.synthetic.main.activity_activity_detail.tv_teacher1
import kotlinx.android.synthetic.main.activity_activity_detail.tv_teacher2
import kotlinx.android.synthetic.main.activity_activity_detail.tv_time

//研学详情
class StudiesDetailActivity : BaseActivity(){

    override fun getLayoutId(): Int = R.layout.activity_activity_detail

    private var curPage = 1
    private var courseId = ""
    private var classId = ""
    override fun initView() {
        courseId = intent.extras.getString("courseId")
        classId = intent.extras.getString("classId")
        darkTextTheme(color(R.color.white))
         topbar.apply {
            setTitle("研学详情")
             setTitleBackColor(color(R.color.white))
//            setTexts("添加", TopBar.Pos.RIGHT)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        rv_list.isNestedScrollingEnabled = false
        pendingAdapter = ActivityDetailItemAdapter(dataList, Box.context)
        rv_list.adapter = pendingAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)
        pendingAdapter.setOnTypeClickListener { obj, position, flag ->
            /* if (flag == ITEM_CLICK) {//研学详情
                 var bundle = Bundle()
                 bundle.putString("type","1")//type 1 职位入口进详情 2 我的应聘入口进详情
                 startActivity_ex<StudiesDetailActivity>("key", bundle)
             }*/
            if (flag == ActivityDetailItemAdapter.agree) {
//                ShowToast("签到")
                activitySign(obj.sid)
            }
        }


        tv_address.setOnClickListener {
            if (TextUtils.isEmpty(latitude) || latitude == "0") {
                ShowToast( "未添加位置信息")
                return@setOnClickListener
            }
//            var latLng = LatLng(latitude.toDouble(), longitude.toDouble())
            UIHelper.goToBaidu(this,latitude,longitude,address)
        }
        iv_call.setOnClickListener {
            if (TextUtils.isEmpty(phone)) {
                ShowToast("电话未填写")
                return@setOnClickListener
            }
            //拨打电话
            val intent = Intent()
            intent.action = Intent.ACTION_DIAL //dial是拨号的意思
            intent.data = Uri.parse("tel:" + phone) //这个tel不能改，后面的数字可以随便改
            Box.context.startActivity(intent)
        }
        iv_call2.setOnClickListener {
            if (TextUtils.isEmpty(phone2)) {
                ShowToast("电话未填写")
                return@setOnClickListener
            }
            //拨打电话
            val intent = Intent()
            intent.action = Intent.ACTION_DIAL //dial是拨号的意思
            intent.data = Uri.parse("tel:" + phone2) //这个tel不能改，后面的数字可以随便改
            Box.context.startActivity(intent)
        }
    }

    //签到
    private fun activitySign(sid: String) {
        Alert.loading(self)
        ApiClient.api.activitySignIn(UserCache.token,sid,courseId).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("签到成功")
                    getDataList()

                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }
    }

    private var latitude=""
    private var longitude=""
    private var address=""
    private var phone=""
    private var phone2=""

    //加载数据
    override fun initData() {
        super.initData()
        getDataList()
    }


    private lateinit var pendingAdapter: ActivityDetailItemAdapter
    private val dataList: MutableList<Studentlist> = mutableListOf()

    private fun getDataList() {
        Alert.loading(self)
        ApiClient.api.getCourseDetail(classId,courseId,UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()

                    latitude=it.latitude
                    longitude=it.longitude
                    address=it.address
                    tv_time.text=it.startTime+"--"+it.endTime
                    tv_address.text=it.address
                    topbar.apply {
                        setTitle(it.name)
                        setTitleBackColor(color(R.color.white))
                        setOnTopBarClickListener { obj, pos ->
                            if (pos == TopBar.Pos.LEFT) {
                                finish()
                            }
                        }
                    }
                    if (it.teacherlist.size == 1) {
                        tv_teacher1.text=it.teacherlist[0].name
                        tv_phone1.text=it.teacherlist[0].phone
                        phone=it.teacherlist[0].phone
                        ll_phone2.visibility= View.GONE
                    }else if (it.teacherlist.size >= 2) {
                        tv_teacher1.text = it.teacherlist[0].name
                        tv_teacher2.text = it.teacherlist[1].name
                        tv_phone1.text=it.teacherlist[0].phone
                        tv_phone2.text=it.teacherlist[1].phone
                        phone=it.teacherlist[0].phone
                        phone2=it.teacherlist[1].phone
                    } else {
                        ll_phone2.visibility= View.GONE
                    }

                    if (it.studentlist.size <= 0) {
//                        multiView.showEmpty()
                    } else {
                        dataList.clear()
                        dataList.addAll(it.studentlist)
                        pendingAdapter.notifyDataSetChanged()
//                        multiView.showContent()
                    }


                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }

    }







    override fun closeMvp() {
    }

}
